package com.global.college.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.global.college.MainActivity;
import com.global.college.R;
import com.global.college.UserData;
import com.global.college.base.BaseActivity;
import com.global.college.base.baselist.EventBusBean;
import com.global.college.constants.Constant;
import com.global.college.data.WeChatTokenBean;
import com.global.college.ui.bean.LoginModel;
import com.global.college.ui.bean.WechatLoginModel;
import com.global.college.ui.bean.WechatUserModel;
import com.global.college.ui.interfaces.ILoginView;
import com.global.college.ui.presenter.LoginPresenter;
import com.global.college.utils.NetworkUtils;
import com.global.college.utils.RxTimer;
import com.global.college.utils.StringUtils;
import com.global.college.utils.ToastUtils;
import com.global.college.views.TitleBar;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 绑定手机号码
 */
public class BindPhoneActivity extends BaseActivity implements TitleBar.TitleBarCallback, ILoginView {

    @BindView(R.id.titleBar)
    TitleBar titleBar;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_sms_code)
    EditText etSmsCode;
    @BindView(R.id.tv_send_code)
    TextView tvSendCode;
    @BindView(R.id.tv_bind_phone)
    TextView tvBindPhone;
    private RxTimer rxTimer;
    private LoginPresenter loginPresenter;
    private String strPhone, strCode;
    private int i = 60;
    private boolean isSend = false;
    String WxOpenId, WxUnionId, accessToken;

    @Override
    public int getContentViewId() {
        return R.layout.activity_bind_phone;
    }

    @Override
    public void onCreateNew(Bundle savedInstanceState) {
        setShowTitleBar(false);
        WxOpenId = getIntent().getStringExtra("WxOpenId");
        WxUnionId = getIntent().getStringExtra("WxUnionId");
        accessToken = getIntent().getStringExtra("accessToken");
        titleBar.setReturnImageResource(R.mipmap.icon_close);
        titleBar.setTitleBarCallback(this);
        loginPresenter = new LoginPresenter(this);
        loginPresenter.setLoginView(this);
    }

    @Override
    public void getInitData() {
        if (rxTimer == null) {
            rxTimer = new RxTimer();
        }
    }

    @Override
    public void onBackPress() {
        finish();
    }

    @Override
    public void onRightPress() {

    }

    @Override
    public void onRightImageClick() {

    }

    @Override
    public void onSearchImageClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_send_code, R.id.tv_bind_phone})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_send_code:
                getCode();
                break;
            case R.id.tv_bind_phone:
                bindPhone();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        rxTimer.cancel();
        rxTimer = null;

    }

    @Override
    public void onLoginSuccess(LoginModel loginModel) {

    }

    @Override
    public void onLoginFail(String msg) {

    }

    @Override
    public void onGetSmsCodeSuccess(Object smsModel) {

    }

    @Override
    public void onGetSmsCodeFail(String msg) {

    }

    /**
     * 获取验证码
     */
    private void getCode() {
        strPhone = etPhone.getText().toString();
        if (!NetworkUtils.isNetworkAvailable(this)) {
            ToastUtils.showToastShort(R.string.your_device_has_not_netword);
            return;
        } else {
            if (StringUtils.isEmpty(strPhone)) {
                ToastUtils.showToastShort(R.string.please_input_you_phone_number);
                return;
            }
            if (!StringUtils.isPhone(strPhone)) {
                ToastUtils.showToastShort(R.string.please_input_you_phone_number);
                return;
            }
            if (isSend) {
                ToastUtils.showToastShort(R.string.please_wait_moment);
                return;
            }
            //发送
            loginPresenter.getSmsCode(strPhone);
            startTimer();
        }
    }

    /**
     * 绑定手机号码
     */
    private void bindPhone() {
        strPhone = etPhone.getText().toString();
        strCode = etSmsCode.getText().toString();
        if (!NetworkUtils.isNetworkAvailable(this)) {
            ToastUtils.showToastShort(R.string.your_device_has_not_netword);
            return;
        } else {
            if (StringUtils
                    .isEmpty(strPhone)) {
                ToastUtils.showToastShort(R.string.please_input_you_phone_number);
                return;
            } else if (StringUtils.isEmpty(strCode)) {
                ToastUtils.showToastShort(
                        R.string.sms_code_not_empty);
                return;
            } else if (!StringUtils.isNumber(strCode)) {
                ToastUtils.showToastShort(R.string.sms_code_format_error);
                return;
            } else {
                //绑定手机号码
//                loginPresenter.wechatLogin(WxOpenId, WxUnionId, accessToken, strCode, "uton_edu_android", strPhone);
            }
        }
    }

    /**
     * 开始计时任务
     */
    private void startTimer() {
        if (rxTimer == null) {
            rxTimer = new RxTimer();
        }
        rxTimer.interval(1000, new RxTimer.RxAction() {
            @Override
            public void action(long number) {
                Log.e("打印时间：", "----" + number);
                if (number == i) {
                    tvSendCode.setText(R.string.resend);
                    isSend = false;
                    tvSendCode.setTextColor(getResources().getColor(R.color.color_fff73043));
                    rxTimer.cancel();

                } else {
                    tvSendCode.setText("(" + (i - number) + ")" + getString(R.string.sended));
                    tvSendCode.setTextColor(getResources().getColor(R.color.color_999999));
                    isSend = true;
                }
            }
        });
    }
}